package com.yaqian.wolf.controller;


import com.yaqian.wolf.comment.JwtUtil;
import com.yaqian.wolf.comment.Result;
import com.yaqian.wolf.entity.Floorlist;
import com.yaqian.wolf.entity.Roomdetail;
import com.yaqian.wolf.entity.User;
import com.yaqian.wolf.repository.RoomdetailRepisitory;
import com.yaqian.wolf.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rent")
public class FloorHandler {
    @Autowired
    private com.yaqian.wolf.repository.FloorlistRepisitory floorlistRepisitory;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private RoomdetailRepisitory roomdetailRepisitory;
    @GetMapping("/floorpage/{page}/{size}")
    public Page<Floorlist> pages(@PathVariable("page")Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        return floorlistRepisitory.findAll(pageable);
    }
    @PostMapping("/addFloor")
    public Result addUser(@RequestBody Floorlist floorlist){
        Result res =new Result();
        try {

            Floorlist ok = floorlist;
            System.out.println(floorlistRepisitory.save(ok));
            //增加所有的room
            String Number =ok.getFloor_Number();
            for (int i = 1; i <=ok.getHeigth() ; i++) {
                for (int j = 1; j <=ok.getRooms(); j++) {
                    Roomdetail room =new Roomdetail();
                    if(j<10)
                        room.setAddress(Number+"-"+i+"0"+j);
                    else     room.setAddress(Number+"-"+i+j);
                    room.setFloor(Number);
                    room.setRent(0);
                    room.setUsed("0");
                    room.setTenancy(0);
                    roomdetailRepisitory.save(room);
                }
            }
            res.success();
            return res;
        }catch (Exception e)
        {
            res.addError();
            return res;
        }
    }

    @GetMapping("/findFloor/{key}")
    public Result Find(@PathVariable String key){
        Result res =new Result();
        try {
            System.out.println(key);
            Floorlist floorlist = floorlistRepisitory.findById(key).get();
            System.out.println("asdas"+floorlist);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("floor", floorlist);
            res.setData(map);
            res.success();
            return res;
        }catch (Exception e)
        {
            res.addError();
            return res;
        }
    }
//

    @GetMapping("/deleteFloor/{key}")
    public Result delete(@PathVariable String key){
        Result res =new Result();
        try {
            System.out.println(key);
           floorlistRepisitory.deleteById(key);
           //删除所有的room
            List ko =roomdetailRepisitory.findAllByFloor(key);
            for (Object k :ko)
            {
                roomdetailRepisitory.deleteById(((Roomdetail) k).getAddress());
            }
            res.success();
            return res;
        }catch (Exception e)
        {
            res.addError("没有这个用户名或者其他原因");
            return res;
        }
    }
}
